﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using CtrlIni;

namespace MultiSherlockGUI
{
    //入库时，库位选择方式。
   
    class TcpServerCfg
    {
        public int iPort = 0;
        public string strIp = "";
    }

    class SqlCfg
    {
        public string strServer = "Localhost";
        public string strDatabase = "db_wms";
        public string strUID = "root";
        public string strPassWord = "";
    }

    class SysCfg
    {
        //程序启动后，Sherlock 的状态
        public bool bAutoRun = false;

     
    }

    class CheckResult
    {
        //是否保存检测结果。
        public bool bSaveCheckCount;

        public int iCount_OK = 0;
        public int iCount_NG = 0;
    }

    class SherlockCfg
    {
        public string strProgramPath;
        public string strImgWindowsName;
        public string strDrivers;
        public double dPicZoom;

        //日志
        public string strPicPath = "";
        public bool bSavePic_OK = false;
        public bool bSavePic_NG = false;
        public int iMaxPicNum = 0;
        //图片最多保存多少天。
        public int iMaxPicLogDay = 60;

        public CheckResult eachCheckResult = new CheckResult();
    }
    

    class UserParam
    {
        //登陆的用户名
        public string strUserName = "";

        //是否成功登陆
        public bool bLogIn = false;
    }

    class GlobalCfg
    {
        public static GUI m_GUI = null;

        //
        public static UserParam m_UserParam = new UserParam();

        public static SqlCfg m_SqlCfg = new SqlCfg();

        public static SysCfg m_SysCfg = new SysCfg();

        public static readonly int m_iSherlockCount = 2;
        public static SherlockCfg []m_SherlockCfg = new SherlockCfg[m_iSherlockCount];

        public static readonly int m_iTcpServerCount = 4;
        public static TcpServerCfg []m_TcpServerCfgArray = new TcpServerCfg[m_iTcpServerCount];

        //////////////////////////////////////////////////////////////////////////
        private static IniFile m_iniFile = new IniFile(Application.StartupPath + "\\HzHeHuaVision.ini");

        public static void LoadCfg()
        {
            //////////////////////////////////////////////////////////////////////////
            m_SqlCfg.strServer = m_iniFile.readString("Sql", "Server","");
            if (m_SqlCfg.strServer.Length < 5)
            {
                m_SqlCfg.strServer = "Localhost";
            }
            m_SqlCfg.strUID = m_iniFile.readString("Sql", "UID","");
            if (m_SqlCfg.strUID.Length < 1)
            {
                m_SqlCfg.strUID = "root";
            }
            m_SqlCfg.strPassWord = m_iniFile.readString("Sql", "PassWord","");
            if (m_SqlCfg.strPassWord.Length < 1)
            {
                m_SqlCfg.strPassWord = "";
            }

          
            //////////////////////////////////////////////////////////////////////////
            m_SysCfg.bAutoRun = m_iniFile.readBool("Sys", "AutoRun", true);

            /////////////////////////////////////////////////////////////////////////
          


            ////////////////////////////////////////////////////////////////////////
            for (int i = 0; i < m_iTcpServerCount; i++)
            {
                m_TcpServerCfgArray[i] = new TcpServerCfg();

                m_TcpServerCfgArray[i].iPort = m_iniFile.readInt("TcpServer_" + i, "Port", 8001 + i);
                m_TcpServerCfgArray[i].strIp = m_iniFile.readString("TcpServer_" + i, "Ip", "");
            }

            //////////////////////////////////////////////////////////////////////////
            for (int i=0;i< m_iSherlockCount; i++)
            {
                m_SherlockCfg[i] = new SherlockCfg();

                m_SherlockCfg[i].strProgramPath = m_iniFile.readString("SherlockProgram_" + i, "SherlockPath", "");
                m_SherlockCfg[i].strImgWindowsName = m_iniFile.readString("SherlockProgram_" + i, "strImgWindowsName", "imgA");
                m_SherlockCfg[i].strDrivers = m_iniFile.readString("SherlockProgram_" + i, "Drivers", "");
                m_SherlockCfg[i].dPicZoom = m_iniFile.readDouble("SherlockProgram_" + i, "PicZoom", -1);
                if (m_SherlockCfg[i].dPicZoom < 0.00001)
                {
                    m_SherlockCfg[i].dPicZoom = -1;
                }
                m_SherlockCfg[i].strPicPath = m_iniFile.readString("SherlockProgram_" + i, "PicPath", "D:\\PicLog\\");
                m_SherlockCfg[i].bSavePic_OK = m_iniFile.readBool("SherlockProgram_" + i, "SavePic_OK", false);
                m_SherlockCfg[i].bSavePic_NG = m_iniFile.readBool("SherlockProgram_" + i, "SavePic_NG", false);
                m_SherlockCfg[i].iMaxPicNum = m_iniFile.readInt("SherlockProgram_" + i, "MaxPicNum", -1);
                m_SherlockCfg[i].iMaxPicLogDay = m_iniFile.readInt("SherlockProgram_" + i, "MaxPicLogDay", -1);

                //结果数据
                m_SherlockCfg[i].eachCheckResult.bSaveCheckCount = m_iniFile.readBool("SherlockProgram_" + i,  "SaveCheckCount", false);
                if (m_SherlockCfg[i].eachCheckResult.bSaveCheckCount == true)
                {
                    m_SherlockCfg[i].eachCheckResult.iCount_OK = m_iniFile.readInt("SherlockProgram_" + i, "Count_OK", 0);
                    m_SherlockCfg[i].eachCheckResult.iCount_NG = m_iniFile.readInt("SherlockProgram_" + i, "Count_NG", 0);
                }
                else
                {
                    m_SherlockCfg[i].eachCheckResult.iCount_NG = 0;
                    m_SherlockCfg[i].eachCheckResult.iCount_OK = 0;
                }
            }
        }

        public static void SavePicLogParam_OK(bool bSave)
        {
            for (int i = 0; i < m_iSherlockCount; i++)
            {
                m_SherlockCfg[i].bSavePic_OK = bSave;
                m_iniFile.writeBool("SherlockProgram_" + i, "SavePic_OK", bSave);
            }
        }

        public static void SaveCheckResult(string strCount_OK, string strCount_NG)
        {
            for (int i = 0; i < m_iSherlockCount; i++)
            {
                //结果数据
                if (m_SherlockCfg[i].eachCheckResult.bSaveCheckCount == true)
                {
                    m_iniFile.writeInt("SherlockProgram_" + i, "Count_OK", m_SherlockCfg[i].eachCheckResult.iCount_OK);
                    m_iniFile.writeInt("SherlockProgram_" + i, "Count_NG", m_SherlockCfg[i].eachCheckResult.iCount_NG);
                }
            }
        }

        public static void SavePicLogParam_NG(bool bSave)
        {
            for (int i = 0; i < m_iSherlockCount; i++)
            {
                m_SherlockCfg[i].bSavePic_NG = bSave;
                m_iniFile.writeBool("SherlockProgram_" + i, "SavePic_NG", bSave);
            }
        }


        public static bool CheckUserPWD(string strUserName, string strPWD)
        {
            bool bRet = false;

            string strRet = m_iniFile.readString("LogIn", strUserName,"");
            if (strRet == strPWD)
            {
                bRet = true;
                m_UserParam.strUserName = strUserName;
                m_UserParam.bLogIn = true;
            }
            else
            {
                bRet = false;
            }

            return bRet;
        }
    }
}
